// 本文件背后的计划可能已经搁置。
import { read, write } from './jsonData.js';

// 自定义排序函数
const customSort = (a, b) => {
    const extractParts = val => {
        const match = val.match(/(\D+)(\d+)/);
        return { text: match[1], num: parseInt(match[2], 10) };
    };

    const aParts = extractParts(a[1]);
    const bParts = extractParts(b[1]);

    // 中文比较：$f(x) = \text{localeCompare}(x_{\text{cn}}, y_{\text{cn}})$
    const textCompare = aParts.text.localeCompare(bParts.text, 'zh-CN');

    // 数字比较：$f(x,y) = x_{\text{num}} - y_{\text{num}}$
    return textCompare !== 0 ? textCompare : aParts.num - bParts.num;
};

const sortJsonFile = async () => {
    const data = await read();
    const sortedData = Object.entries(data).sort(customSort);
    const sortedObj = Object.fromEntries(sortedData);
    write(sortedObj);
};

sortJsonFile();
